home *** CD-ROM | disk | FTP | other *** search
Text File | 1996-02-19 | 7.5 KB | 213 lines | [TEXT/CWIE] |
-
- #include "FileIterators.h"
- #include "FSSpecification.h"
-
- //========================================================================================
- // CLASS TAbstractFileIterator
- //========================================================================================
-
- //----------------------------------------------------------------------------------------
- // TAbstractFileIterator::~TAbstractFileIterator
- //----------------------------------------------------------------------------------------
- TAbstractFileIterator::~TAbstractFileIterator()
- {
- } // TAbstractFileIterator::~TAbstractFileIterator
-
-
- //========================================================================================
- // CLASS TCatalogSearchIterator
- //========================================================================================
-
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::TCatalogSearchIterator
- //----------------------------------------------------------------------------------------
- TCatalogSearchIterator::TCatalogSearchIterator(SInt32 vRefNum, CInfoPBRec& searchSpec, CInfoPBRec& searchMask, SInt32 flags)
- {
- fCatSearchBuffer = nil;
- fResultsBuffer = nil;
- fCurrentEntry = 0;
- fEntriesInBuffer = 0;
- fCatsearchHasMoreDataToReturn = false;
-
- this->InitializeParamBlock(vRefNum, searchSpec, searchMask, flags);
- } // TCatalogSearchIterator::TCatalogSearchIterator
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::TCatalogSearchIterator:
- //----------------------------------------------------------------------------------------
- TCatalogSearchIterator::TCatalogSearchIterator(SInt32 vRefNum, unsigned char* fileName, OSType fileType /* = 0*/, OSType fileCreator /*= 0*/, SInt32 flags /*= 0*/)
- {
- fCatSearchBuffer = nil;
- fResultsBuffer = nil;
- fCurrentEntry = 0;
- fEntriesInBuffer = 0;
- fCatsearchHasMoreDataToReturn = false;
-
- CInfoPBRec searchSpec;
- CInfoPBRec searchMask;
-
- //
- // Zero out important fields in the search mask
- //
- searchMask.hFileInfo.ioFlAttrib = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdLocation.v = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdLocation.h = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdFldr = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdFlags = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdType = 0;
- searchMask.hFileInfo.ioFlFndrInfo.fdCreator = 0;
-
- if(fileType != 0)
- {
- searchSpec.hFileInfo.ioFlFndrInfo.fdType = fileType;
- searchMask.hFileInfo.ioFlFndrInfo.fdType = 0xFFFFFFFF;
- }
-
- if(fileCreator != 0)
- {
- searchSpec.hFileInfo.ioFlFndrInfo.fdCreator = fileCreator;
- searchMask.hFileInfo.ioFlFndrInfo.fdCreator = 0xFFFFFFFF;
- }
-
- searchSpec.hFileInfo.ioNamePtr = (StringPtr)fileName;
-
- this->InitializeParamBlock(vRefNum, searchSpec, searchMask, flags);
- } // TCatalogSearchIterator::TCatalogSearchIterator
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::~TCatalogSearchIterator:
- //----------------------------------------------------------------------------------------
- TCatalogSearchIterator::~TCatalogSearchIterator()
- {
- if(fCatSearchBuffer != nil)
- DisposeHandle(fCatSearchBuffer);
- if(fResultsBuffer != nil)
- DisposeHandle((Handle)fResultsBuffer);
-
- } // TCatalogSearchIterator::~TCatalogSearchIterator
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::More:
- //----------------------------------------------------------------------------------------
- Boolean TCatalogSearchIterator::More() const
- {
- return ((fEntriesInBuffer > fCurrentEntry) || fCatsearchHasMoreDataToReturn);
- } // TCatalogSearchIterator::More
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::Next:
- //----------------------------------------------------------------------------------------
- void TCatalogSearchIterator::Next()
- {
- if(this->More())
- {
- ++fCurrentEntry;
- if(fCurrentEntry >= fEntriesInBuffer)
- this->ReadMoreInfoViaCatSearch();
- }
- } // TCatalogSearchIterator::Next
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::Current:
- //----------------------------------------------------------------------------------------
- TFSSpecification TCatalogSearchIterator::Current() const
- {
- return TFSSpecification((*fResultsBuffer)[fCurrentEntry]);
- } // TCatalogSearchIterator::Current
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::InitializeParamBlock:
- //----------------------------------------------------------------------------------------
- void TCatalogSearchIterator::InitializeParamBlock(SInt32 vRefNum, CInfoPBRec& searchSpec, CInfoPBRec& searchMask, SInt32 flags)
- {
- //
- // Set up search spec and search mask
- //
- fSearchSpec = searchSpec;
- fSearchMask = searchMask;
-
- //
- // Make sure that the directory bits are set if our flags
- // indicate that we should search only files or only directories
- //
- if((flags & (kSeachFilesAndDirectories)) == kSeachFilesAndDirectories)
- {
- fSearchMask.hFileInfo.ioFlAttrib &= ~ioDirMask;
- }
- else if((flags & (kSeachFilesOnly | kSearchDirectoriesOnly)) != 0)
- {
- if(flags & kSeachFilesOnly)
- fSearchSpec.hFileInfo.ioFlAttrib &= ~ioDirMask;
- else
- fSearchSpec.hFileInfo.ioFlAttrib |= ioDirMask;
- fSearchMask.hFileInfo.ioFlAttrib |= ioDirMask;
- }
-
- //
- // Set up paramater block to point to search spec and search mask
- //
- fParamBlock.ioSearchInfo1 = &fSearchSpec;
- fParamBlock.ioSearchInfo2 = &fSearchMask;
- fParamBlock.ioNamePtr = nil;
- fParamBlock.ioVRefNum = vRefNum;
-
- fParamBlock.ioCatPosition.initialize = 0; // Start at the beginning
- fParamBlock.ioSearchTime = 300; // Time out every 1/2 sec
- fParamBlock.ioActMatchCount = 0;
-
- //
- // Always search based on finder info and folder attributes;
- // also search based on name if ioNamePtr is not nil.
- //
- fParamBlock.ioSearchBits = fsSBFlFndrInfo | fsSBFlAttrib;
- if(fSearchSpec.hFileInfo.ioNamePtr != nil)
- fParamBlock.ioSearchBits |= (((flags & kNameMustMatchExactly) != 0) ? fsSBFullName : fsSBPartialName);
-
- //
- // Set up buffers
- //
- fParamBlock.ioOptBufSize = 0x4000L;
- fParamBlock.ioReqMatchCount = (flags & kOnlyOneMatch) != 0 ? 1 : 10;
-
- fCatSearchBuffer = NewHandle(fParamBlock.ioOptBufSize);
- fResultsBuffer = (FSSpec**)NewHandle(sizeof(FSSpec) * (fParamBlock.ioReqMatchCount + 1));
-
- if((fCatSearchBuffer != nil) && (fResultsBuffer != nil))
- {
- HLock(fCatSearchBuffer);
- fParamBlock.ioOptBuffer = *fCatSearchBuffer;
- HLock((Handle)fResultsBuffer);
- fParamBlock.ioMatchPtr = *fResultsBuffer;
-
- fCatsearchHasMoreDataToReturn = true;
- this->ReadMoreInfoViaCatSearch();
- }
- } // TCatalogSearchIterator::InitializeParamBlock
-
- //----------------------------------------------------------------------------------------
- // TCatalogSearchIterator::ReadMoreInfoViaCatSearch:
- //----------------------------------------------------------------------------------------
- void TCatalogSearchIterator::ReadMoreInfoViaCatSearch()
- {
- fCurrentEntry = 0;
- fEntriesInBuffer = 0;
-
- while((fEntriesInBuffer == 0) && (fCatsearchHasMoreDataToReturn == true))
- {
- OSErr err = PBCatSearchSync(&fParamBlock);
- fEntriesInBuffer = fParamBlock.ioActMatchCount & 0x0000FFFF;
- fCatsearchHasMoreDataToReturn = (err == noErr);
- }
-
- //
- // fParamBlock.ioReqMatchCount == 1 iff we are interested
- // in only the first match from catsearch
- //
- if(fParamBlock.ioReqMatchCount > 1)
- fCatsearchHasMoreDataToReturn = false;
- } // TCatalogSearchIterator::ReadMoreInfoViaCatSearch
-
-
-
-